package com.klikmee.sms.chat.web.senders;

import java.util.Date;

import com.klikmee.sms.cef.CoreAdapter;
import com.klikmee.sms.chat.web.dao.AnswerDAO;
import com.klikmee.sms.services.chat.model.Answer;
import com.klikmee.sms.services.chat.model.Question;

/**
 * <p>
 * Base class for {@link MessageSender} implementations. Exposes {@link CoreAdapter} and {@link AnswerDAO} instances and {@link #persistAnswer(Question, String, Long, boolean)} method
 * </p>
 * 
 * @author makcro
 * 
 */
public abstract class AbstractMessageSender implements MessageSender {

	/**
	 * {@link CoreAdapter} instance used to send messages via core platform.
	 */
	protected CoreAdapter coreAdapter = null;
	protected AnswerDAO answerDAO = null;

	public AbstractMessageSender() {
		super();
	}

	public void setCoreAdapter(CoreAdapter coreAdapter) {
		this.coreAdapter = coreAdapter;
	}

	public void setAnswerDAO(AnswerDAO answerDAO) {
		this.answerDAO = answerDAO;
	}

	/**
	 * Persists {@link Answer} object into database
	 * 
	 * @param question
	 * @param messageText
	 * @param refCoreId
	 * @param visible
	 *            TODO
	 */
	protected void persistAnswer(Question question, String messageText, Long refCoreId, boolean visible) {
		Answer answer = new Answer();
		answer.setAnswer(messageText);
		answer.setCoreID(refCoreId);
		answer.setDateAnswered(new Date());
		answer.setFromNumber(question.getToNumber());
		answer.setQuestion(question);
		answer.setToNumber(question.getFromNumber());
		answer.setVisable(visible);

		this.answerDAO.create(answer);
	}

}